package com.ybxx.base.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.base.model.Community;
import com.ybxx.base.model.FeesItem;
import com.ybxx.base.model.FeesType;
import com.ybxx.base.model.Hydropower;
import com.ybxx.base.model.HydropowerItem;
import com.ybxx.base.model.ResHydropower;
import com.ybxx.base.model.Type;
import com.ybxx.base.service.CommunityService;
import com.ybxx.base.service.HydropowerItemService;
import com.ybxx.base.service.HydropowerService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.User;

public class HydropowerAction extends BaseAction {
	@Resource
	private CommunityService communityService;
	@Resource
	private HydropowerService hydropowerService;
	@Resource
	private HydropowerItemService hydropowerItemService;
	private List<Community> communitys;
	private List<HydropowerItem> hydropowerItems;
	private String cid;
	private boolean editsign=true;
	private Community community;
	private Hydropower hydropower;
	private String tids;
	private String rmins;
	private String rmaxs;
	private String prices;
	private String msg="succ";
	/*查看*/
	public String view(){
		community=communityService.queryFees(cid);
		hydropowerItems=hydropowerItemService.queryHydropowerItem(cid);
		hydropower=hydropowerService.queryHydropower(cid);
		return "view";
	}
	/*删除*/
	public String remove(){
		try{
			hydropower=hydropowerService.queryHydropower(cid);
			hydropowerService.remove(hydropower);
		}catch(Exception ex){
			msg="卫生费删除失败.";
		}
		return "succ";
	}
	
	/*保存*/
	public String save(){
		try{
			if(hydropower!=null && StringUtils.isEmpty(hydropower.getHcode())){
				hydropower.setHcode(UUIDGenerator.getUUID());
			}
			if(!StringUtils.isEmpty(tids)){
				String[] tid=tids.split(",");
				String[] rmin=rmins.split(",");
				String[] rmax=rmaxs.split(",");
				String[] price=prices.split(",");
				for(int i=0;i<tid.length;i++){
					HydropowerItem hydropowerItem=new HydropowerItem();
					hydropowerItem.setRhid(UUIDGenerator.getUUID());
					hydropowerItem.setHydropower(hydropower);
					hydropowerItem.setType(new Type(tid[i]));
					hydropowerItem.setRmin(rmin[i]);
					hydropowerItem.setRmax(rmax[i]);
					hydropowerItem.setPrice(price[i]);
					hydropower.getHydropowerItems().add(hydropowerItem);
				}
			}
			hydropowerService.save(hydropower);
		}catch(Exception ex){
			ex.printStackTrace();
			msg="水电费保存失败.";
		}
		return "succ";
	}
	/*加载修改，新增的数据*/
	public String load(){
		if(cid==null || "".equals(cid)){//新增
			User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
			communitys=communityService.queryNotHydropower(user.getLoginid());
		}else{//修改
			editsign=false;
			community=communityService.queryFees(cid);
			hydropowerItems=hydropowerItemService.queryHydropowerItem(cid);
			hydropower=hydropowerService.queryHydropower(cid);
		}
		return "load";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		/**获取当前登录者*/
		User user=(User)super.getSession().getAttribute(SystemConstants.SESSION_CURRENT_USER);
		communitys=communityService.queryHydropowerPage(filter,user.getLoginid());
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	public List<Community> getCommunitys() {
		return communitys;
	}
	public void setCommunitys(List<Community> communitys) {
		this.communitys = communitys;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public boolean isEditsign() {
		return editsign;
	}
	public void setEditsign(boolean editsign) {
		this.editsign = editsign;
	}
	public Community getCommunity() {
		return community;
	}
	public void setCommunity(Community community) {
		this.community = community;
	}
	public Hydropower getHydropower() {
		return hydropower;
	}
	public void setHydropower(Hydropower hydropower) {
		this.hydropower = hydropower;
	}
	public String getTids() {
		return tids;
	}
	public void setTids(String tids) {
		this.tids = tids;
	}
	public String getRmins() {
		return rmins;
	}
	public void setRmins(String rmins) {
		this.rmins = rmins;
	}
	public String getRmaxs() {
		return rmaxs;
	}
	public void setRmaxs(String rmaxs) {
		this.rmaxs = rmaxs;
	}
	public String getPrices() {
		return prices;
	}
	public void setPrices(String prices) {
		this.prices = prices;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<HydropowerItem> getHydropowerItems() {
		return hydropowerItems;
	}
	public void setHydropowerItems(List<HydropowerItem> hydropowerItems) {
		this.hydropowerItems = hydropowerItems;
	}
}
